<template>
  <div class="elm-edit">
    <elm-page-bb>
      <el-form ref="model" :model="model" size="small" label-width="100px">
        <elm-row>
          <el-form-item label="进货批次" prop="name" :rules="input_rule">
            <el-input v-model="model.name" ></el-input>
          </el-form-item>
          <el-form-item label="货物名称" prop="dict" :rules="input_rule">
            <elm-select style="width: 100%" v-model="model.dict" url="/business/jxc/goods/dict/GetData"></elm-select>
          </el-form-item>
        </elm-row>
        <elm-row>
          <el-form-item label="规格/型号" prop="specification">
            <el-input v-model="model.specification"></el-input>
          </el-form-item>
          <el-form-item label="入库数量" prop="quantity" :rules="[{trigger: 'blur' ,validator: validateNumber}]">
            <el-input v-model.number="model.quantity" :key="model.unit">
              <template slot="append" v-if="model.unit">
                <div style="width: 25px;text-align: center;">{{model.unit}}</div>
              </template>
            </el-input>
          </el-form-item>
        </elm-row>
        <elm-row>
          <el-form-item label="入库时间" prop="in_time">
            <el-date-picker v-model="model.in_time"  type="datetime" value-format="yyyy-MM-dd HH-mm-ss" style="width: 100%"></el-date-picker>
          </el-form-item>
          <el-form-item label="入库员" prop="in_person">
            <elm-ei v-model="model.in_person"></elm-ei>
          </el-form-item>
        </elm-row>
        <elm-row>
          <el-form-item label="进货价" prop="price" :rules="[{trigger: 'blur' ,validator: validateNumber}]">
            <el-input v-model.number="model.price"></el-input>
          </el-form-item>
          <el-form-item label="税率" prop="tex" :rules="[{trigger: 'blur' ,validator: validateNumber}]">
            <el-input v-model.number="model.tex">
              <template slot="append">
                <div style="width: 25px;text-align: center;">{{"%"}}</div>
              </template>
            </el-input>
          </el-form-item>
        </elm-row>
        <elm-row>
          <el-form-item label="生产日期" prop="made_date">
            <el-date-picker v-model="model.made_date"  type="date" value-format="yyyy-MM-dd" style="width: 100%"></el-date-picker>
          </el-form-item>
          <el-form-item label="有效期至" prop="shelf_life">
            <el-date-picker v-model="model.shelf_life"  type="date" value-format="yyyy-MM-dd" style="width: 100%"></el-date-picker>
          </el-form-item>
        </elm-row>
        <el-form-item label="厂家" prop="manufacturer">
          <el-input v-model="model.manufacturer"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="model.remarks" type="textarea" :rows="2"></el-input>
        </el-form-item>
      </el-form>
      <template #bottom>
        <el-button type="success" size="small" @click="save">保存并继续</el-button>
        <el-button type="success" size="small" @click="submit">立即保存</el-button>
        <el-button size="small" @click="close">取消</el-button>
      </template>
    </elm-page-bb>
  </div>
</template>

<script>
export default {
  extends: elmVueBase,
  data() {
    return {
      model: {
        id: elm.newGuid(),
        dict:null,
        in_person:null,
        unit:null
      },
      resData:{
        quantity:null,
      },
      ruleEnable:false
    }
  },
  async created() {
    this.model.in_time=this.get_time();
    this.model.in_person=await this.get_user_ei();
  },
  destroyed() {
    this.request.callVue.tablebinding();
  },
  computed:{
    input_rule(){
      if(this.ruleEnable)
        return [{ required: true, message: '必须填写'}];
      else
        return [];
    }
  },
  methods: {
    async save() {
      var res = await this.upload();
      if (res) {
        this.v('保存成功,请继续。')
        this.ruleEnable=false;
        this.model = {
          id: elm.newGuid(),
          in_time: this.model.in_time,
          in_person: this.model.in_person,
          remarks: this.model.remarks,
          name:this.model.name
        }
      }
      else {
        this.error('保存失败，请稍后尝试。');
      }
    },
    async submit() {
      var res = await this.upload();
      if (res) {
        this.v('保存成功。')
        this.close();
        this.request.callVue.tablebinding();
      }
      else {
        this.error('保存失败，请稍后尝试。');
      }
    },
    get_time(){
      var now=new Date();
      return now.getFullYear() + '-'
          + (now.getMonth() + 1).toString().padStart(2, '0') + '-'
          + now.getDate().toString().padStart(2, '0')+ ' '
          + now.getHours().toString().padStart(2, '0') + ':'
          + now.getMinutes().toString().padStart(2, '0') + ':'
          + now.getSeconds().toString().padStart(2, '0');
    },
    async get_user_ei(){
      var res = await elm.post('/system/login/GetUserEi');
      //console.log(res);
      if(res.length>0)
        return res[0].emp_code;
      else
        return "";
    },
    async getResData(Val){
      //console.log(Val+'\n');
      var res=await elm.post('/business/jxc/goods/dict/GetData',{id:Val})
      //console.log("结果：",res);
      if(Val!=null&&Val!=""&&res.pagedata.length>0){
        this.resData=res.pagedata[0];
      }
    },
    async upload(){
      this.ruleEnable=true;
      if(this.model.quantity<0)this.model.quantity=0;
      await this.getResData(this.model.dict);
      await this.$refs["model"].validate();
      this.model.create_time = this.get_time();
      this.model.create_person = await this.get_user_ei();
      var res = await elm.post('/business/jxc/goods/in/Upsert', this.model);
      this.resData.quantity+=Number(this.model.quantity);
      var re = await elm.post('/business/jxc/goods/dict/Upsert', this.resData);
      this.model.last_update_time=this.get_time();
      var r = await elm.post('/business/jxc/goods/Upsert', this.model);
      return (res==true && re ==true && r==true);
    }
  },
  watch: {
    'model.dict': async function (newVal) {
      //console.log(`dict changed to: ${newVal}`);
      var res = await elm.post('/business/jxc/goods/dict/GetGoodsData', {id: newVal});
      //console.log("结果：",res);
      if (res.length > 0) {
        Object.assign(this.model, res[0]);
      }
      await this.getResData(newVal);
      //console.log(this.model,this.resData);
    },
  }
}
</script>